package com.yygo.service.impl;

import com.yygo.dao.LoanPersonDao;
import com.yygo.model.LoanPerson;
import com.yygo.model.LoanRequest;
import com.yygo.model.enums.RequestStatus;
import com.yygo.service.LoanPersonManager;
import com.yygo.service.LoanRequestManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by tiansha on 2015/7/8.
 */
@Service("loanPersonManager")
public class LoanPersonManagerImpl extends GenericManagerImpl<LoanPerson, Long> implements LoanPersonManager {

    private LoanPersonDao loanPersonDao;

    @Autowired
    public LoanPersonManagerImpl(LoanPersonDao loanPersonDao) {
        super(loanPersonDao);
        this.loanPersonDao = loanPersonDao;
    }

    @Autowired
    private LoanRequestManager loanRequestManager;

    @Override
    public List<LoanRequest> findByIdNo(LoanRequest loanRequest) {
        Set<Long> ids =  loanPersonDao.findByIdNo(loanRequest);
        List<LoanRequest> requests = new ArrayList<>();
        for (Long id : ids) {
            LoanRequest loanRequest1 = loanRequestManager.get(id);
            if (!loanRequest1.getStatus().equals(RequestStatus.draft)) {
                requests.add(loanRequest1);
            }
        }
        return requests;
    }

    @Override
    public List<LoanRequest> findByIdentity(LoanRequest loanRequest, int days) {
        Set<Long> ids = loanPersonDao.findByIdentity(loanRequest, days);
        List<LoanRequest> requests = new ArrayList<>();
        for (Long id : ids) {
            LoanRequest request = loanRequestManager.get(id);
            if (!request.getStatus().equals(RequestStatus.draft)) {
                requests.add(request);
            }
        }
        return requests;
    }

    @Override
    public List<LoanRequest> findByProperty(LoanRequest loanRequest, String property, int days) {
        Set<Long> ids = loanPersonDao.findByProperty(loanRequest, property, days);
        List<LoanRequest> requests = new ArrayList<>();
        for (Long id : ids) {
            LoanRequest request = loanRequestManager.get(id);
            if (!request.getStatus().equals(RequestStatus.draft)) {
                requests.add(request);
            }
        }
        return requests;
    }

    @Override
    public List<LoanRequest> findByAddress(LoanRequest loanRequest, int days) {
        Set<Long> ids = loanPersonDao.findByAddress(loanRequest, days);
        List<LoanRequest> requests = new ArrayList<>();
        for (Long id : ids) {
            LoanRequest request = loanRequestManager.get(id);
            if (!request.getStatus().equals(RequestStatus.draft)) {
                requests.add(request);
            }
        }
        return requests;
    }

    @Override
    public List<LoanRequest> findByCompanyAddress(LoanRequest loanRequest, int days) {
        Set<Long> ids = loanPersonDao.findByCompanyAddress(loanRequest, days);
        List<LoanRequest> requests = new ArrayList<>();
        for (Long id : ids) {
            LoanRequest request = loanRequestManager.get(id);
            if (!request.getStatus().equals(RequestStatus.draft)) {
                requests.add(request);
            }
        }
        return requests;
    }

    @Override
    public List<LoanRequest> findByUrgentNo(LoanRequest loanRequest, int days) {
        Set<Long> ids = loanPersonDao.findByUrgentNo(loanRequest, days);
        List<LoanRequest> requests = new ArrayList<>();
        for (Long id : ids) {
            LoanRequest request = loanRequestManager.get(id);
            if (!request.getStatus().equals(RequestStatus.draft)) {
                requests.add(request);
            }
        }
        return requests;
    }
}
